package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.PackingDao;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.domain.cargo.Packing;
import cn.itcast.domain.cargo.PackingExample;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.domain.cargo.ShippingExample;
import cn.itcast.service.cargo.ShippingService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class ShippingServiceImpl implements ShippingService {
    @Autowired
    private ShippingDao shippingDao;

    @Autowired
    private PackingDao packingDao;

    /**
     * 分页查询所有的委托书
     *
     * @param loginCompanyId
     * @param pageNum
     * @param pageSize
     */
    @Override
    public PageInfo<Shipping> findAll(String loginCompanyId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        ShippingExample shippingExample = new ShippingExample();
        shippingExample.createCriteria().andCompanyIdEqualTo(loginCompanyId);
        List<Shipping> list = shippingDao.selectByExample(shippingExample);

        PageInfo<Shipping> pageInfo = new PageInfo<>(list);

        return pageInfo;
    }

    /**
     * 根据id删除委托书
     *
     * @param shippingOrderId
     */
    @Override
    public void deleteBySId(String shippingOrderId) {
        shippingDao.deleteByPrimaryKey(shippingOrderId);
    }

    /**
     * 增加委托书
     *
     * @param shipping
     */
    @Override
    public void addShipping(Shipping shipping) {
        //封装剩余的数据
          //设置状态
          shipping.setState(0);
          shipping.setCreateTime(new Date());
          shippingDao.insertSelective(shipping);
         //需要修改装箱单的状态
        Packing packing = new Packing();
        packing.setPackingListId(shipping.getShippingOrderId());
        packing.setState(1);
        packingDao.updateByPrimaryKeySelective(packing);
    }

    /**
     * 通过id查找指定的委托书
     *
     * @param shippingOrderId
     */
    @Override
    public Shipping findBySOId(String shippingOrderId) {

        return shippingDao.selectByPrimaryKey(shippingOrderId);
    }
}
